using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Haidou.Wpf.Util.Converters;

public class DoubleToThickness : IMultiValueConverter
{
	public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
	{
		if (values.Length < 2)
		{
			return new Thickness(0);
		}

		bool pos = (parameter as string) == "+";
		double left = values[0] as double? ?? values[0] as int? ?? 0;
		double top = values[1] as double? ?? values[1] as int? ?? 0;

		if (values.Length < 4)
		{
			return pos
				? new Thickness(Math.Abs(Math.Max(0, left)), Math.Abs(Math.Max(0, top)), Math.Abs(Math.Max(0, left)), Math.Abs(Math.Max(0, top)))
				: (object)new Thickness(Math.Abs(Math.Min(0, left)), Math.Abs(Math.Min(0, top)), Math.Abs(Math.Min(0, left)), Math.Abs(Math.Min(0, top)));
		}

		double right = values[2] as double? ?? values[2] as int? ?? 0;
		double bottom = values[3] as double? ?? values[3] as int? ?? 0;

		return pos
			? new Thickness(Math.Abs(Math.Max(0, left)), Math.Abs(Math.Max(0, top)), Math.Abs(Math.Max(0, right)), Math.Abs(Math.Max(0, bottom)))
			: (object)new Thickness(Math.Abs(Math.Min(0, left)), Math.Abs(Math.Min(0, top)), Math.Abs(Math.Min(0, right)), Math.Abs(Math.Min(0, bottom)));
	}

	public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
	{
		throw new NotImplementedException();
	}
}